﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Sunny.Entity;
using Sunny.Model.RequestParam;
using Sunny.Service;
using Sunny.Util.Attributes;
using Sunny.Util.Enums;

namespace Sunny.Api.Controllers
{
    [Route("/system/dict/data")]
    [ApiController]
    public class SysDictDataController
    {
        private readonly DictDataService _dictDataService;

        public SysDictDataController(DictDataService dictDataService)
        {
            _dictDataService = dictDataService;
        }
        #region 根据字典类型查询字典数据信息

        [Route("type/{dictType}")]
        [HttpGet]
        public async Task<Result> DictType([FromRoute]string dictType)
        {
            List<SysDictData> sysDictData = await _dictDataService.GetDictDataByType(dictType);
            if(sysDictData == null)
            {
                sysDictData = new List<SysDictData>();
            }
            return Result.Success(sysDictData);
        }

        #endregion

        #region 用户字典数据列表
        [HasPermission("system:dict:list")]
        [HttpGet("list")]
        public async Task<Result> List([FromQuery]DictDataListParam param)
        {
            (List<SysDictData> list,int total) = await _dictDataService.List(param);
            return Result.Success(new
            {
                rows = list,
                total
            });
        }
        #endregion

        #region 查询详细信息
        [HasPermission("system:dict:query")]
        [HttpGet("{dictCode}")]
        public async Task<Result> Detail([FromRoute]long dictCode)
        {
            SysDictData sysDictData = await _dictDataService.GetById(dictCode);
            return  Result.Success(sysDictData);
        }
        #endregion
        
        #region 新增字典数据信息
        [HasPermission("system:dict:add")]
        [LogInfo(Title = "字典数据管理",BusinessType = BusinessType.Insert)]
        [HttpPost]
        public async Task<Result> Add([FromBody]DictDataEditParam param)
        {
            await _dictDataService.Add(param);
            return  Result.Success();
        }
        #endregion
        
        #region 更新字典数据信息
        [HasPermission("system:dict:edit")]
        [LogInfo(Title = "字典数据管理",BusinessType = BusinessType.Update)]
        [HttpPut]
        public async Task<Result> Edit([FromBody]DictDataEditParam param)
        {
            await _dictDataService.Update(param);
            return  Result.Success();
        }
        #endregion
        
        #region 删除字典数据信息
        [HasPermission("system:dict:remove")]
        [LogInfo(Title = "字典数据管理",BusinessType = BusinessType.Delete)]
        [HttpDelete("{dictCodes}")]
        public async Task<Result> Delete([FromRoute]string dictCodes)
        {
            await _dictDataService.Delete(dictCodes);
            return  Result.Success();
        }
        #endregion
    }
}